package com.zeng.ssm.service.support.async;

import com.zeng.ssm.dal.mapper.AsyncTaskRecordDao;
import com.zeng.ssm.dal.model.AsyncTaskRecord;
import com.zeng.ssm.service.support.async.dto.AsyncTaskRecordBean;
import com.zeng.ssm.service.util.ApplicationContextUtil;
import com.zeng.ssm.util.DateUtil;
import com.zeng.ssm.util.JsonUtil;
import org.springframework.util.Assert;

/**
 * 异步任务-提交工具类
 *
 * @author zengwanlin
 * @date 2023-04-04T17:30
 */
public class AsyncTaskUtil {

    /**
     * 提交任务
     *
     * @param recordBean
     * @param extraParam
     */
    public static void submit(AsyncTaskRecordBean recordBean, Object extraParam) {
        Assert.hasText(recordBean.getTaskCode(), "任务码不能为空");
        Assert.hasText(recordBean.getTaskName(), "任务名称不能为空");

        AsyncTaskRecordDao asyncTaskRecordDao = ApplicationContextUtil.getBean(AsyncTaskRecordDao.class);

        AsyncTaskRecord record = new AsyncTaskRecord();
        record.setTaskCode(recordBean.getTaskCode());
        record.setTaskName(recordBean.getTaskName());
        record.setBusiDate(DateUtil.getDate8());
        record.setContractId(recordBean.getContractId());
        record.setDetailId(recordBean.getDetailId());
        record.setResourceId(recordBean.getResourceId());
        record.setResourceSubId(recordBean.getResourceSubId());
        if (extraParam == null) {
            record.setParamJson("{}");
        } else {
            record.setParamJson(JsonUtil.toJson(extraParam));
        }
        record.setExecuteTimes(0);
        record.setStatus(AsyncTaskConstant.ASYNC_TASK_STATUS_0);
        record.setLockOwner(AsyncTaskExecutorLauncher.DEFAULT_LOCK_OWNER);
        record.setLockTtl(AsyncTaskExecutorLauncher.DEFAULT_LOCK_TTL);
        record.setCreateTime(DateUtil.getDateTime());
        asyncTaskRecordDao.insertSelective(record);
    }

}
